package cn.deepmax.spring.cloud.system.sale.vo;

import cn.deepmax.spring.cloud.common.web.AjaxResponse;
import cn.deepmax.spring.cloud.common.web.SelfValidated;
import cn.deepmax.spring.cloud.system.sale.entity.Order;
import lombok.Data;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

import java.util.Optional;
@Data
public class AddOrderRequest implements SelfValidated {

    private Long userId;
    private Long productId;
    private Long price;
    private Integer quantity;
    private String description;

    public static Order valueOf(AddOrderRequest r){
        Order o = new Order();
        o.setProductId(r.productId);
        o.setUserId(r.userId);
        o.setPrice(r.price);
        o.setQuantity(r.quantity);
        o.setDescription(r.description);
        return o;
    }

    /**
     * @param applicationContext
     * @return empty if no errors
     */
    @Override
    public Optional<AjaxResponse> validate(ApplicationContext applicationContext) {
        Chain chain = chain();

        chain.assertTrue(()->productId!=null && productId>0, "product id should >0")
                .assertTrue(price!=null && price>0, "price should >0")
                .assertTrue(quantity!=null && quantity>0, "quantity should >0")
                .assertTrue(StringUtils.hasText(description),"description should has text.");
        return chain.validate();
    }
}
